package delay;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;

import java.nio.charset.StandardCharsets;
import java.util.List;

public class Consumer {

    public static void main(String[] args) throws Exception{
        //1.创建消费者
        DefaultMQPushConsumer dd = new DefaultMQPushConsumer("dd");

        //2.设置NameService地址
        dd.setNamesrvAddr("localhost:9876");

        //3.订阅topic，指定tag标签
        dd.subscribe("sync","holle");

        //4.注册消息监听器
        dd.registerMessageListener(new MessageListenerConcurrently() {
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
                for (MessageExt messageExt : list) {
                    //把当前的生产发送的消息装成字符串 并这是UTF-8
                    String string = new String(messageExt.getBody(), StandardCharsets.UTF_8);
                    System.out.println(string);

                }
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;


            }
        });
        //5.启动
        dd.start();
    }
}
